package cn.hxzy.servlet.admin;

import cn.hxzy.entity.News;
import cn.hxzy.entity.User;
import cn.hxzy.entity.vo.PageObj;
import cn.hxzy.service.NewsService;
import cn.hxzy.service.UserService;

import javax.naming.Name;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/showNews")
public class ShowNewsServlet extends HttpServlet {
   private NewsService newsService = new NewsService();
   private UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
        String id = req.getParameter("id");
        String date = req.getParameter("date");
        String users = req.getParameter("users");
        System.out.println(id);
        System.out.println(date);
        System.out.println(users);
        PageObj<News> pageObj = new PageObj<>();
        String page = req.getParameter("page");
        String size = req.getParameter("size");
        String name = req.getParameter("name");
        System.out.println(name);

        if (page != null && page != "") {
            pageObj.setPage(Integer.parseInt(page));
        }
        if (size != null && size != "") {
            pageObj.setSize(Integer.parseInt(size));
        }
        if (id != null && id != "") {//id查询
            req.setAttribute("id",id);
            PageObj<News> news = newsService.findByIdWithUserName(pageObj,Integer.parseInt(id));
            System.out.println(news);
            req.setAttribute("showNews", news);
            req.getRequestDispatcher("/WEB-INF/jsp/admin/showNews.jsp").forward(req, resp);
        }
        else if (name != null && name != "") {//名字查询
            req.setAttribute("name",name);
            PageObj<News> news = newsService.findByNameWithUser(pageObj,name );
            System.out.println("*******************\n"+news+"\n******************");
            req.setAttribute("showNews", news);
            req.getRequestDispatcher("/WEB-INF/jsp/admin/showNews.jsp").forward(req, resp);
        }
        else if (date != null && date != "") {//创建时间查询
            req.setAttribute("date",date);
            PageObj<News> news = newsService.findByDateWithUserDate(pageObj,date);
            System.out.println(news);
            req.setAttribute("showNews", news);
            req.getRequestDispatcher("/WEB-INF/jsp/admin/showNews.jsp").forward(req, resp);
        }
        else if (users != null && users != "") {//user名字查询
            req.setAttribute("users",users);
            PageObj<News> news = newsService.findByUserNameWithUser(pageObj,users);
            System.out.println(news);
            req.setAttribute("showNews", news);
            req.getRequestDispatcher("/WEB-INF/jsp/admin/showNews.jsp").forward(req, resp);
        }
        PageObj<News> news = newsService.findAll(pageObj);
        List<User> user = userService.findAll();
        req.setAttribute("showNews", news);
        req.setAttribute("users", user);
        System.out.println("**************输出的信息\n"+news+"****************");
        System.out.println("**************输出的信息\n"+user+"****************");
        req.getRequestDispatcher("/WEB-INF/jsp/admin/showNews.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
